<?php


namespace app\admin\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Config;
use think\Db;
use think\Exception;
use think\Session;

class Citizens extends Base
{
    //查看市民账户日志
    public function log(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $where = '1=1';
        $keyword = '';

        if (!empty($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $where .= " and (b.nickname like '%".$keyword."%' or b.realName like '%".$keyword."%')";
        }
        $thisWechat = Session::get("thisWechat");
        $where .=" and b.uniacid = ".$thisWechat['id'];

        if(!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                $list = Db::name("admin_fans_log")->alias("a")
                    ->join("admin_fans b","a.fans_id = b.id")
                    ->field("a.*,b.realName,b.nickname")
                    ->where($where)->limit($start,$limit)
                    ->order("a.id desc")
                    ->select();

                $count = Db::name("admin_fans_log")->alias("a")
                    ->join("admin_fans b","a.fans_id = b.id")
                    ->field("a.*,b.realName,b.nickname")
                    ->where($where)->count("*");

                $type_status = [1=>"增加",2=>"减少",3=>"管理员设置"];
                foreach ($list as $k=>$v){
                    $list[$k]['status'] = $type_status[$v['type_status']];
                    $list[$k]['type'] = $v['type']=="balance"?"余额":"积分";
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        $assign = [
            "keyword"=>$keyword,
        ];
        $this->assign($assign);

        return $this->fetch('log');
    }

    //市民账户日志
    public function createlog($data){
        $data['addtime'] = time();
        $data['auto'] = Session::get("auto");
        $data['operator_id'] = Session::get("id");
        Db::name("admin_fans_log")->insert($data);
    }

    //充值操作
    public function changes(){
        $type = $_POST['type'];
        $id = $_POST['id'];
        $common = new Common();
        try{
            $status = $_POST['status']; //操作
            $number = $_POST['number']; //数量
            $remark = empty($_POST['remark'])?'':$_POST['remark']; //备注
            switch ($type){
                case "balance":
                    $balance = Db::name("admin_fans")->where(['id'=>$id])->value("balance");
                    if($number<0){
                        throw new Exception("非法操作");
                    }
                    if($status==2){
                        if($balance - $number<0){ throw new Exception("非法操作");}
                        Db::name("admin_fans")->where(['id'=>$id])->setDec("balance",$number);
                        $after_number = $balance-$number;

                        $log['remark'] = "用户【" . Session::get("user_name") . "】操作市民可用余额,余额减少【".$number."】,操作粉丝id【".$id."】";
                    }elseif ($status==1){
                        Db::name("admin_fans")->where(['id'=>$id])->setInc("balance",$number);
                        $log['remark'] = "用户【" . Session::get("user_name") . "】操作市民可用余额,余额增加【".$number."】,操作粉丝id【".$id."】";
                        $after_number = $balance+$number;
                    }else{
                        Db::name("admin_fans")->where(['id'=>$id])->update(['balance'=>$number]);
                        $log['remark'] = "用户【" . Session::get("user_name") . "】操作市民可用余额,最终余额【".$number."】,操作粉丝id【".$id."】";
                        $after_number = $number;
                    }
                    break;
                case "integral":
                    $balance = Db::name("admin_fans")->where(['id'=>$id])->value("integral");

                    if($number<0){
                        throw new Exception("非法操作");
                    }
                    if($status==2){
                        if($balance - $number<0){ throw new Exception("非法操作");}
                        Db::name("admin_fans")->where(['id'=>$id])->setDec("integral",$number);
                        $after_number = $balance-$number;

                        $log['remark'] = "用户【" . Session::get("user_name") . "】操作市民积分,积分减少【".$number."】,操作粉丝id【".$id."】";
                    }elseif ($status==1){
                        Db::name("admin_fans")->where(['id'=>$id])->setInc("integral",$number);
                        $log['remark'] = "用户【" . Session::get("user_name") . "】操作市民积分,积分增加【".$number."】,操作粉丝id【".$id."】";
                        $after_number = $balance+$number;
                    }else{
                        Db::name("admin_fans")->where(['id'=>$id])->update(['integral'=>$number]);
                        $log['remark'] = "用户【" . Session::get("user_name") . "】操作市民积分,最终积分【".$number."】,操作粉丝id【".$id."】";
                        $after_number = $number;
                    }
                    break;
            }

            //记录账户日志
            $data['fans_id'] = $id;
            $data['remark'] = $remark;
            $data['type'] = $type;
            $data['type_status'] = $status;
            $data['number'] = $number;
            $data['before_number'] = $balance;
            $data['after_number'] = $after_number;
            $this->createlog($data);

            //记录操作日志
            $log['controller'] = "Citizens";
            $log['methods'] = "changes";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;

    }


    //粉丝列表
    public function index(){

        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $idCard = '';
        $mobile = '';
        $realName = "";
        $time = "";
        $where = [];
        if(!empty($_GET['idCard'])){
            $where['idCard'] = ['like',"%".$_GET['idCard']."%"];
            $idCard = $_GET['idCard'];
        }
        if(!empty($_GET['mobile'])){
            $where['mobile'] = ['like',"%".$_GET['mobile']."%"];
            $mobile = $_GET['mobile'];
        }
        if(!empty($_GET['realName'])){
            $where['realName'] = ['like',"%".$_GET['realName']."%"];
            $realName = $_GET['realName'];
        }
        if(!empty($_GET['time'])){
            $time = $_GET['time'];
            $serach = explode(' - ',$time);
            $starttime = strtotime($serach[0]);
            $endttime = strtotime($serach[1]." 23:59:59");
            $where['addtime'] = [["gt",$starttime],["elt",$endttime]];
        }
        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];


        if(!empty($_GET['type'])) {

            try{
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;

                //获取粉丝表信息
                $list = Db::name("admin_fans")->where($where)->limit($start,$limit)->select();
                $count = Db::name("admin_fans")->where($where)->count("*");
                //$sex = Config::get("sex");
                //屏类型
                $sex = Db::name("config_attribute")->where(['key'=>"sex"])->value("value");
                $sex = explode("|",$sex);
                foreach ($sex as $k=>$v){
                    $sex[$k+1] = $v;
                }
                unset($sex[0]);
                $commom = new Common();
                foreach ($list as $k=>$v){
                    $v['avatar'] = empty($v['avatar']) ? "/static/images/nopic-107.png" : $v['avatar'];
                    $list[$k]['avatar'] = "<img src='".$v['avatar']."' style='height:50px'>";
                    $v['faceUrl'] = empty($v['faceUrl']) ? "/static/images/nopic-107.png" : $v['faceUrl'];
                    $list[$k]['faceUrl'] = "<img src='".$v['faceUrl']."' style='height:50px'>";
                    $list[$k]['sexVal'] =  empty($v['sex'])?"":$sex[$v['sex']];
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);

                    if(!empty($v['idCard'])){
                        $list[$k]['age'] = $commom->getAgeByID($v['idCard']);
                    }else{
                        $list[$k]['age'] = 0;
                    }
                }

                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;

        }




        $assign = [
            "time" => $time,
            "idCard"=>$idCard,
            "mobile"=>$mobile,
            "realName"=>$realName
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //详细信息
    public function detail(){
        //$sex = Config::get("sex");
        //屏类型
        $sex = Db::name("config_attribute")->where(['key'=>"sex"])->value("value");
        $sex = explode("|",$sex);
        foreach ($sex as $k=>$v){
            $sex[$k+1] = $v;
        }
        unset($sex[0]);
        //查询详信息
        $where['id'] = $_POST['id'];
        $detail = Db::name("admin_fans")->where($where)->find();
        $detail['avatar'] = empty($detail['avatar'])?"/static/images/nopic-107.png":$detail['avatar'];
        $detail['sexVal'] =  $sex[$detail['sex']];
        $this->assign("detail",$detail);
        return view("detail");
    }


}